package com.source.lightfowl.basedata.dao;

import com.source.lightfowl.basedata.entity.PoultryCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 12:51
 */
public interface PoultryCategoryDao extends JpaRepository<PoultryCategory,Integer>, JpaSpecificationExecutor<PoultryCategory> {
    @Modifying
    @Transactional
    @Query("delete from PoultryCategory s where s.id in (?1)")
    void deleteBatch(ArrayList<Integer> ids);

    @Modifying
    @Transactional
    @Query("delete from PoultryCategory s where s.id=(?1)")
    void deleteById(String id);
    @Query(
            nativeQuery = true,
            value = "SELECT * FROM poultry_category WHERE id=?1"
    )
    Optional<PoultryCategory> findOptionalById(String id);

    @Query(
            nativeQuery = true,
            value = "SELECT * FROM poultry_category WHERE id=?1"
    )
    PoultryCategory findListById(String id);

    @Query(
            nativeQuery = true,
            value = "SELECT * FROM poultry_category WHERE number=?1"
    )
    PoultryCategory findByIdNumber(String id);

    @Query(
            nativeQuery = true,
            value = "SELECT * FROM poultry_category WHERE id=?1"
    )
    PoultryCategory findById(String id);


    @Query(
            nativeQuery = true,
            value = "SELECT *" +
                    /*"PoultryCategory.id," +
                    "PoultryCategory.number," +
                    "PoultryCategory.name " +*/
                    "FROM poultry_category " +
                    "WHERE slaughterHouseId=(?1) " +
                    "ORDER BY id DESC"
    )
    List<PoultryCategory> findByHouseId(int id);

    @Query(
            nativeQuery = true,
            value = "SELECT COUNT(id) FROM poultry_category " +
                    "WHERE slaughterHouseId=?1 " +
                    "GROUP BY slaughterHouseId"
    )
    BigInteger findNumOfPoultry(int id);

    @Query(
            nativeQuery = true,
            value = "SELECT name from poultry_category " +
                    "WHERE id=?1"
    )
    String findNameById(String id);

    @Query(
            nativeQuery = true,
            value = "SELECT * FROM poultry_category " +
                    "WHERE parentId=?1"
    )
    List<PoultryCategory> findByParentId(String id);

    @Query(
            nativeQuery = true,
            value = "SELECT id FROM poultry_category " +
                    "WHERE parentId=?1 AND slaughterHouseId=?2"
    )
    List<String> findIdByParentAndHouseId(String parentId, String houseId);

    @Query(
            nativeQuery = true,
            value = "SELECT id FROM poultry_category " +
                    "WHERE parentId=?1"
    )
    List<String> findIdByParent(String parentId);

    @Modifying
    @Transactional
    @Query(nativeQuery = true,
    value = "delete from poultry_category where slaughterHouseId in (?1)")
    void deleteBySlaughterHouseId(ArrayList<String> slist);
}
